<?php
define('FPDF_FONTPATH', 'font/');
require('./font/fpdf.php');
include("include/config.inc.php");
include("amountToWords.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $pdf=new FPDF('P','mm','A4');   //Create new pdf file
  $pdf->Open();     //Open file
  $pdf->SetAutoPageBreak(false);  //Disable automatic page break
  $pdf->AddPage();  //Add first page
	
  $i         = 0; 
  $rowHeight = 6;
  $yAxis     = 89;
  
  $totalAmm      = 0 ;
  $totalAll      = 0 ;
  $tax           = 0 ;
  $allTax        = 0 ;
  $serviceTax    = 0 ;
  $AllserviceTax = 0 ;
  $roundOff      = 0 ;
  $netTotal      = 0 ; 

  $yAxis = $yAxis + $rowHeight;
  
  $selBillMaster = "SELECT billMasterId, billNo, DATE_FORMAT(date,'%d-%m-%Y') AS billDate, partyName, total, taxInPrecentage, serviceTax, roundOff, netTotal, notes
	                    FROM billmaster
	                    JOIN party ON billmaster.partyId = party.partyId
	                   WHERE billMasterId = ".$_GET['billMasterId'];
  $selBillMasterRes = mysql_query($selBillMaster);
  while($mRow = mysql_fetch_array($selBillMasterRes))
  {
    $billMasterId    = $mRow['billMasterId'];
    $billNo          = $mRow['billNo'];
    $billDate        = $mRow['billDate'];
    $partyName       = $mRow['partyName'];
    $total           = number_format(round($mRow['total'], 2), 2);
    $taxInPrecentage = $mRow['taxInPrecentage'];
    $serviceTax      = number_format(round($mRow['serviceTax'], 2), 2);
    $roundOff        = number_format(round($mRow['roundOff'], 2), 2);
    $netTotal        = number_format(round($mRow['netTotal'], 0), 0);
    $notes           = $mRow['notes'];
    $netTotalInWords = ucwords(words(round($mRow['netTotal'],0)));
    $serviceTax      = $mRow['taxInPrecentage'];
    $roundOff        = $mRow['roundOff'];
    $netTotal        = $mRow['netTotal'];
    
    pageHeader();
    pageFooterlast();
    $selBillDetail = "SELECT billdetail.*, DATE_FORMAT(outgoingmaster.date,'%d-%m-%y') AS outDate, outChallenNo,incoming.challan AS inChallenNo,materialName
                        FROM billdetail
                        JOIN outgoingmaster ON billdetail.outGoingMasterId = outgoingmaster.outGoingMasterId
                        JOIN incoming ON outgoingmaster.incomingId = incoming.incomingId
                        JOIN material ON incoming.materialId = material.materialId
                       WHERE billMasterId = ".$mRow['billMasterId'];
    $selBillDetailRes = mysql_query($selBillDetail);
    while($dRow = mysql_fetch_array($selBillDetailRes))
    {
    	if ($i == 22)
	    {
	      $pdf->AddPage();
	      pageHeader();
	      pageFooterlast();
	      //Go to next row
	      $yAxis = 95;
	
	      //Set $i variable to 0 (first row)
	      $i = 1;
	
	    }
    	$outDate      = $dRow['outDate'];
    	$outChallenNo = $dRow['outChallenNo'];
    	$inChallenNo  = $dRow['inChallenNo'];
    	$materialName = $dRow['materialName'];
    	$finalProcess = $dRow['finalProcess'];
    	$outQuantity  = $dRow['outQuantity'];
    	$outWieght    = $dRow['outWieght'];
    	$outRate      = $dRow['outRate'];
    	$amount       = number_format(round($dRow['amount'], 2), 2);
    	$totalAll    += $dRow['amount'];
      $tax          = $mRow['taxInPrecentage'];
      $roundOff     = $mRow['roundOff'];
      
      $allTax   = $tax;
      $totalAmm = $totalAll;
      $serviceTax   =  $tax * $totalAmm /100 ;
      $roundOff = $roundOff;
      $netTotal = $totalAmm + $serviceTax + $roundOff ;
    	
      $pdf->SetTextColor(0,0,0);
      $pdf->SetFont('Times','',12);
      $pdf->SetXY(5,$yAxis);
      $pdf->Cell(12,6, $outChallenNo, 1,0,'C');
      $pdf->Cell(23,6, $outDate,      1,0,'L');
      $pdf->Cell(15,6, $inChallenNo,  1,0,'L');
      $pdf->Cell(30,6, $materialName, 1,0,'L');
      $pdf->Cell(35,6, $finalProcess, 1,0,'L');
      $pdf->Cell(20,6, $outWieght,    1,0,'R');
      $pdf->Cell(15,6, $outQuantity,  1,0,'R');
      $pdf->Cell(25,6, $outRate,      1,0,'R');
      $pdf->Cell(25,6, $amount,       1,0,'R');
      
      $yAxis = $yAxis + $rowHeight;
      $i = $i + 1;
    }
    
    $pdf->SetFont('Arial', '', 10);
    $pdf->SetXY(5,228);
    $pdf->MultiCell(115, 5, 'Rs. In Words : '. $netTotalInWords.'only', 0,'L');
    
    $pdf->SetTextColor(0,0,0);
	  $pdf->SetFont('Arial', '', 12);
	  $pdf->SetXY(120,228);
	  $pdf->Cell(60, 7, 'Sub Totol :', 0, 0, 'R', 0);
	  $pdf->Cell(25, 7, $totalAmm,        0, 0, 'R', 0);
	  $pdf->SetFont('Arial', '', 9);
	  $pdf->SetXY(120,235);
	  $pdf->Cell(60, 7, 'Service Tax @ + Edu.Cess @ : '. $allTax .'%', 0, 0, 'R', 0);
	  $pdf->SetFont('Arial', '', 12);
	  $pdf->SetXY(180,235);
	  $pdf->Cell(25, 7, $serviceTax, 0, 0, 'R', 0);
	  $pdf->SetXY(120,242);
	  $pdf->Image('./logo/rounfOff.jpg',140,243,40,0);
	  $pdf->Cell(85, 7, $roundOff, 0, 0, 'R', 0);
	  
	  $pdf->SetFont('Arial', 'B', 15);
	  $pdf->SetXY(120,250);
	  $pdf->Cell(60, 10, 'Net Total :', 1, 0, 'R', 0);
	  $pdf->Cell(25, 10, $netTotal,     1, 0, 'R', 0);
	  
	  $pdf->SetXY(180,228);
	  $pdf->Cell(25, 32, '', 1, 0, 'L', 0);
	  $pdf->SetXY(180,250);
	  $pdf->Cell(25, 10, '', 1, 0, 'L', 0);
  }
	$pdf->Output();  
}
function pageHeader()
{
  global $pdf;
  global $yAxis;
  global $partyName;
  global $billNo;
  global $billDate;
  
  //$pdf->Image('./logo/logo2.jpg',5,0,200,40);
  $pdf->Line(5,45,205,45);
  $pdf->Image('./logo/logo3.jpg',7,46,203,0);
  
  $pdf->Line(17,95,17,227);
  $pdf->Line(40,95,40,227);
  $pdf->Line(55,95,55,227);
  $pdf->Line(85,95,85,227);
  $pdf->Line(120,95,120,227);
  $pdf->Line(140,95,140,227);
  $pdf->Line(155,95,155,227);
  $pdf->Line(180,95,180,227);
  $pdf->Line(205,57,205,227);
  $pdf->Line(5,57,5,227);
  
  $pdf->Line(5,57,205,57);
  $pdf->Line(5,227,205,227);

  $pdf->SetTextColor(0,0,0);
  $pdf->SetFont('Arial', 'B', 12);
  $pdf->SetXY(5,58);
  $pdf->Cell(120,6, 'M/s. : '.$partyName,   0, 0, 'L', 0);
  $pdf->Cell(80, 6, 'Invoice No. : '.$billNo, 0, 0, 'L', 0);
  $pdf->SetXY(125,68);
  $pdf->Cell(80, 6, 'Invoice Date : '.$billDate,  0, 0, 'L', 0);
  $pdf->Line(16,63,120,63);
  $pdf->Line(150,63,200,63);
  $pdf->Line(152,73,200,73);

  $pdf->SetFont('Arial', 'B', 12);
  $pdf->SetXY(5,83);
  $pdf->MultiCell(35, 6, '   Chalan Date:              No.',    1, 'L');
  $pdf->SetFont('Arial', 'B', 8);
  $pdf->SetXY(40,83);
  $pdf->MultiCell(15, 4, 'Your Challan No', 1, 'C');
  $pdf->SetFont('Arial', 'B', 12);
  $pdf->SetXY(55,83);
  $pdf->Cell(30, 12, '   Part Name',       1, 'C');
  $pdf->Cell(35, 12, '  Process Name',    1, 'C');
  $pdf->Cell(20, 12, '  Weight',          1, 'C');
  $pdf->Cell(15, 12, '  Pcs.',            1, 'C');
  $pdf->MultiCell(25, 6, 'Rate Kg. Pcs.',   1, 'C');
  $pdf->SetXY(180,83);
  $pdf->Cell(25, 12, '   Amount',          1, 'C');
}

function pageFooterlast()
{
  global $pdf;
  
  $pdf->SetFont('Arial', 'B', 12);
  $pdf->SetXY(120,243);
  $pdf->Cell(85 , 5, 'To Be Continue', 0, 0, 'C', 0);
  
  $pdf->SetTextColor(0,0,0);
  $pdf->SetFont('Arial', '', 12);
  $pdf->SetXY(5,228);
  $pdf->Cell(115, 32, '', 1, 0, 'L', 0);
  
  $pdf->SetFont('Arial', 'B', 12);
  $pdf->SetXY(5,239);
  $pdf->Cell(115, 6, 'Terms & Condition', 0, 0, 'L', 0);
  
  $pdf->SetFont('Arial', '', 10);
  $pdf->SetXY(5,244);
  $pdf->Cell(115, 6, '1. E.&.O.E.', 0, 0, 'L', 0);
  $pdf->SetXY(5,249);
  $pdf->Cell(115, 6, '2. Interest @ 18% Will Be charged On Amount Overdue After 30 Day .', 0, 0, 'L', 0);
  $pdf->SetXY(5,254);
  $pdf->Cell(115, 6, '3. Subjects To Rajkot Jurisdiction.', 0, 0, 'L', 0);
  
  $pdf->SetTextColor(0,0,0);
  $pdf->SetFont('Arial', '', 12);
  $pdf->SetXY(5,261);
  $pdf->Cell(200, 30, '', 1, 0, 'L', 0);
  
  $pdf->SetXY(5,175);
  $pdf->Cell(1, 180, 'I.T.PAN NO. : ABHFS 9017 R', 0, 0, 'L', 0);
  $pdf->SetXY(5,182);
  $pdf->Cell(1, 180, 'GST TIN :        24092501705  Dt.  14/08/2007', 0, 0, 'L', 0);
  $pdf->SetXY(5,188);
  $pdf->Cell(1, 180, 'CST TIN :        24592501705  Dt.  14/08/2007', 0, 0, 'L', 0);
  $pdf->SetXY(5,194);
  $pdf->Cell(1, 180, 'SERVICES TAX NO. :ABHFS 9017 R ST001', 0, 0, 'L', 0);
  $pdf->SetXY(140,281);
  $pdf->Cell(44, 10, 'Authorised  Signatory', 0, 0, 'L', 0);
  $pdf->SetXY(113,261);
  $pdf->Cell(11, 10, 'For,  ', 0, 0, 'L', 0);
  $pdf->SetTextColor(230,0,0);
  $pdf->SetFont('Arial', 'B', 13);
  $pdf->SetXY(123,261);
  $pdf->Cell(11, 10, 'Shakti Heat-Treaters & Engineers', 0, 0, 'L', 0);
}
?>